home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / pyshared / computerjanitorapp / app_tests.py < prev    next >
Encoding:
Python Source  |  2009-03-04  |  5.9 KB  |  178 lines

  1. # app_tests.py - unit tests for app.py
  2. # Copyright (C) 2008, 2009  Canonical, Ltd.
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, version 3 of the License.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program.  If not, see <http://www.gnu.org/licenses/>.
  15.  
  16.  
  17. import os
  18. import shutil
  19. import tempfile
  20. import unittest
  21.  
  22. import computerjanitor
  23. import computerjanitorapp
  24.  
  25.  
  26. class MockUI(object):
  27.  
  28.     def __init__(self, testcase, app, pm):
  29.         self.testcase = testcase
  30.         
  31.     def run(self, options, args):
  32.         self.testcase.ui_ran = True
  33.  
  34.  
  35. class MockPackage(object):
  36.  
  37.     def __init__(self, downloadable=False):
  38.         self.candidateDownloadable = downloadable
  39.  
  40.  
  41. class MockApt(dict):
  42.  
  43.     def __init__(self):
  44.         self._depcache = self
  45.         
  46.     def ReadPinFile(self, filename):
  47.         pass
  48.  
  49.     def Cache(self):
  50.         return self
  51.  
  52.  
  53. class MockCruft(object):
  54.  
  55.     def __init__(self, name):
  56.         self.name = name
  57.  
  58.     def get_name(self):
  59.         return self.name
  60.  
  61.  
  62. class ApplicationTests(unittest.TestCase):
  63.  
  64.     def setUp(self):
  65.         self.mock_apt = MockApt()
  66.         self.app = computerjanitorapp.Application(apt=self.mock_apt)
  67.         self.app.apt_cache["dash"] = MockPackage(downloadable=True)
  68.         self.app.apt_cache["gzip"] = MockPackage(downloadable=True)
  69.  
  70.     def testSetsUpAptAttributeCorrectly(self):
  71.         self.assertEqual(self.app.apt, self.mock_apt)
  72.         
  73.     def testSetsUpAndReturnsState(self):
  74.         self.assert_(self.app.state)
  75.  
  76.     def testSetsUpAptCacheWhenRequested(self):
  77.         self.assertNotEqual(self.app.apt_cache, None)
  78.  
  79.     def testSetsOptionDefaultsCorrectly(self):
  80.         options, args = self.app.parse_options(args=[])
  81.         self.assertEqual(args, [])
  82.         self.assertEqual(options.all, None)
  83.         self.assertEqual(options.state_file, 
  84.                          "/var/lib/computer-janitor/state.dat")
  85.         self.assertEqual(options.no_act, None)
  86.  
  87.     def testAcceptsDashDashAllOption(self):
  88.         options, args = self.app.parse_options(args=["--all"])
  89.         self.assertEqual(options.all, True)
  90.  
  91.     def testAcceptsDashDashStateFileOption(self):
  92.         options, args = self.app.parse_options(args=["--state-file=foo"])
  93.         self.assertEqual(options.state_file, "foo")
  94.  
  95.     def testAcceptsDashDashNoActOption(self):
  96.         options, args = self.app.parse_options(args=["--no-act"])
  97.         self.assertEqual(options.no_act, True)
  98.  
  99.     def testRunsUserInterface(self):
  100.  
  101.         def pm_class(app, plugin_dirs):
  102.             self.pm_ran = True
  103.  
  104.         def ui_class(app, pm):
  105.             return MockUI(self, app, pm)
  106.  
  107.         self.pm_ran = False
  108.         self.ui_ran = False
  109.         self.app.run(ui_class=ui_class, plugin_manager_class=pm_class)
  110.         self.assert_(self.ui_ran)
  111.  
  112.     def testAcceptsAptCacheWhenEssentialPackagesAreThere(self):
  113.         self.assertEqual(self.app.verify_apt_cache(), None)
  114.  
  115.     def testRejectsAptCacheWhenDashIsMissing(self):
  116.         del self.app.apt_cache["dash"]
  117.         self.assertRaises(computerjanitor.Exception, 
  118.                           self.app.verify_apt_cache)
  119.  
  120.     def testRejectsAptCacheWhenGzipIsMissing(self):
  121.         del self.app.apt_cache["gzip"]
  122.         self.assertRaises(computerjanitor.Exception, 
  123.                           self.app.verify_apt_cache)
  124.  
  125.     def testRejectsAptCacheWhenDashIsNotDownloadable(self):
  126.         self.app.apt_cache["dash"].candidateDownloadable = False
  127.         self.assertRaises(computerjanitor.Exception, 
  128.                           self.app.verify_apt_cache)
  129.  
  130.     def testRejectsAptCacheWhenGzipIsNotDownloadable(self):
  131.         self.app.apt_cache["gzip"].candidateDownloadable = False
  132.         self.assertRaises(computerjanitor.Exception, 
  133.                           self.app.verify_apt_cache)
  134.  
  135.     def testSetsDefaultListOfWhitelistDirectoriesCorrectly(self):
  136.         self.assert_("/etc/computer-janitor.d" in self.app.whitelist_dirs)
  137.  
  138.     def testReturnsEmptyWhitelistByDefault(self):
  139.         dirname = tempfile.mkdtemp()
  140.         whitelist = self.app.whitelisted_cruft(dirnames=[dirname])
  141.         shutil.rmtree(dirname)
  142.         self.assertEqual(whitelist, [])
  143.  
  144.     def testDoesNotMindNonExistentWhitelistDirectory(self):
  145.         dirname = tempfile.mkdtemp()
  146.         subdir = os.path.join(dirname, "foo")
  147.         whitelist = self.app.whitelisted_cruft(dirnames=[subdir])
  148.         shutil.rmtree(dirname)
  149.         self.assertEqual(whitelist, [])
  150.  
  151.     def testReadsWhitelistFilesCorrectly(self):
  152.         dirname = tempfile.mkdtemp()
  153.         temp1 = os.path.join(dirname, "foo.whitelist")
  154.         temp2 = os.path.join(dirname, "foo.whitelist~")
  155.  
  156.         file(temp1, "w").write("deb:foo\n")
  157.         file(temp2, "w").write("deb:bar\n")
  158.  
  159.         whitelist = self.app.whitelisted_cruft(dirnames=[dirname])
  160.         shutil.rmtree(dirname)
  161.         self.assertEqual(whitelist, ["deb:foo"])
  162.  
  163.     def testFindsCorrectWhitelistFilesInDotDDirectory(self):
  164.         dirname = tempfile.mkdtemp()
  165.         file(os.path.join(dirname, "foo.whitelist"), "w").write("deb:foo\n")
  166.         file(os.path.join(dirname, "foo.whitelist~"), "w").write("deb:bar\n")
  167.         list = self.app.whitelist_files([dirname])
  168.         shutil.rmtree(dirname)
  169.         self.assertEqual(list, [os.path.join(dirname, "foo.whitelist")])
  170.  
  171.     def testRemovesWhitelistedCruftCorrectly(self):
  172.         crufts = [MockCruft("deb:foo"), MockCruft("deb:bar")]
  173.         dirname = tempfile.mkdtemp()
  174.         file(os.path.join(dirname, "foo.whitelist"), "w").write("deb:foo\n")
  175.         crufts2 = self.app.remove_whitelisted(crufts, dirnames=[dirname])
  176.         shutil.rmtree(dirname)
  177.         self.assertEqual(crufts2, crufts[1:])
  178.